
(defun p39-2 (s)
  (loop for i from (+ 3 4 5) below s
	  collect (if (> (length (integral-triangle i)) 0)
			      (cons i (length (integral-triangle i))))))
(defun p39 (s)
  (defun p39-inner (k init-result)
	(cond ((> k s) init-result)
		  ((= (length (integral-triangle k)) 0) (p39-inner (1+ k) init-result ))
		  (t (p39-inner (1+ k) (list init-result (list k (length (integral-triangle k))))))))
  (p39-inner 12 '()))

;;; triangle with integral length s
(defun integral-triangle (s)
  (mapcar
	(lambda (x)
	  (append x (list (apply '- (cons s x)))))
  (remove-if-not
	(lambda (x)
	  (= (sum-of-expt x) (expt (apply '- (cons s x)) 2)))
	(combin s))))

(defun integral-triangle-2 (s)
  (loop for x from 2 to s
		append (loop for y from 1 to x
					  append (if (= (+ (expt x 2) (expt y 2)) (expt (- s x y) 2))
								  (list x y (- s x y))))))


(defun sum-of-expt (mn-list)
  (apply '+ (mapcar (lambda (x) (expt x 2)) mn-list)))

(defun combin (s)
  (loop for x from 2 to s
		append (loop for y from 1 to (1- x)
					  collect (list x y))))

;;;TODO
;;; list a b c in the combin function
;;; refracting others
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(format t "~a~%" (integral-triangle 120))
(format t "~a~%" (p39 1000))
